package Test;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import Member1.Data6;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Input extends JPanel {
	private JLabel lblNewLabel;
	private JLabel lblNewLabel_1;
	private JLabel lblNewLabel_2;
	private JTextField name;
	private JTextField sex;
	private JTextField age;
	private JButton btnNewButton;

	Data6[] d;
	/**
	 * Create the panel.
	 */
	public Input() {
		setLayout(null);
		add(getLblNewLabel());
		add(getLblNewLabel_1());
		add(getLblNewLabel_2());
		add(getName1());
		add(getSex());
		add(getAge());
		add(getBtnNewButton());

	}

	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("이름");
			lblNewLabel.setBounds(40, 41, 57, 15);
		}
		return lblNewLabel;
	}
	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("성별");
			lblNewLabel_1.setBounds(40, 78, 57, 15);
		}
		return lblNewLabel_1;
	}
	private JLabel getLblNewLabel_2() {
		if (lblNewLabel_2 == null) {
			lblNewLabel_2 = new JLabel("나이");
			lblNewLabel_2.setBounds(40, 118, 57, 15);
		}
		return lblNewLabel_2;
	}
	private JTextField getName1() {
		if (name == null) {
			name = new JTextField();
			name.setBounds(123, 38, 116, 21);
			name.setColumns(10);
		}
		return name;
	}
	private JTextField getSex() {
		if (sex == null) {
			sex = new JTextField();
			sex.setBounds(123, 75, 116, 21);
			sex.setColumns(10);
		}
		return sex;
	}
	private JTextField getAge() {
		if (age == null) {
			age = new JTextField();
			age.setBounds(123, 115, 116, 21);
			age.setColumns(10);
		}
		return age;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("저장");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					Input i = new Input();
					i.setD(d);
					boolean flag = true; // 모든 값을 입력했을 때
					if(name.getText().equals("")) flag=false;     //  빈공간이 생기면 안돌아간다.   플래그가 참이라면 입력되고 
					else if(sex.getText().equals("")) flag=false;
			      
			        else if(age.getText().equals("")) flag=false;
			        
			        if(flag){
			        	Data6 data = new Data6();
			        	data.name = name.getText();
			        	data.sex = sex.getText();
			        	
			        	data.age = Integer.parseInt(age.getText());
					for(int pos=0;pos<d.length;pos++){
						if(d[pos] == null){
							d[pos] = data;
							
							name.setText("");
							sex.setText("");
							
							
							age.setText("");
							
							name.requestFocus(); // 커서이동
							
							break;
						} 
					}
			
		
			        	
			        }
				
				}
			});
			btnNewButton.setBounds(142, 173, 97, 23);
		}
		return btnNewButton;
	}
	public void setD(Data6[] d){
		this.d = d;
	}
}
